var utilCity = require('../../utils/cityData.js');
Component({
  properties: {
    value: {
      type: Array,
      value: [0, 0, 0],
      observer: "onValueChange"
    },
    isShow: {
      type: Boolean,
      value: false,
      observer: "onShow"
    },
    isShowArea: {
      type: Boolean,
      value: true
    }
  },
  data: {
    cityData: utilCity.cityData,
    showPicker: false,
    tempProvincePos: [0],
    tempCityPos: [0],
    tempAreaPos: [0]
  },
  methods: {
    onTouchmask: function (event) { },
    onCacnelClick(e) {
      this.triggerEvent('cancelclick', {})
    },
    onSureClick(e) {
      var valueCode = [0, 0, 0];
      var valueName = ['', '', ''];
      if (this.data.cityData != null && this.data.cityData.length > this.data.tempProvincePos) {
        if (this.data.cityData[this.data.tempProvincePos].childs != null && this.data.cityData[this.data.tempProvincePos].childs.length > this.data.tempCityPos) {
          valueCode = [
            this.data.cityData[this.data.tempProvincePos].id,
            this.data.cityData[this.data.tempProvincePos].childs[this.data.tempCityPos].id,
            this.data.cityData[this.data.tempProvincePos].childs[this.data.tempCityPos].childs[this.data.tempAreaPos].id
          ];
          valueName = [this.data.cityData[this.data.tempProvincePos].value,
          this.data.cityData[this.data.tempProvincePos].childs[this.data.tempCityPos].value,
          this.data.cityData[this.data.tempProvincePos].childs[this.data.tempCityPos].childs[this.data.tempAreaPos].value
          ];
        } else {
          valueCode = [this.data.cityData[this.data.tempProvincePos].id, '0', '0'];
          valueName = [this.data.cityData[this.data.tempProvincePos].value, '', ''];
        }
      }

      this.triggerEvent('sureclick', {
        valueCode: valueCode,
        valueName: valueName
      })
    },
    province_onChange: function (e) {
      this.setData({
        tempProvincePos: e.detail.value,
        tempCityPos: [0],
        tempAreaPos: [0]
      })
    },
    city_onChange: function (e) {
      this.setData({
        tempCityPos: e.detail.value,
        tempAreaPos: [0]
      })
    },
    //区县变动
    area_onChange: function (e) {
      this.setData({
        tempAreaPos: e.detail.value
      })
    },
    onValueChange(newVal, oldVal, changePath) {
      this.onValue();
    },
    onValue() {
      var tempProvincePos = 0;
      var tempCityPos = 0;
      var tempAreaPos = 0;
      if (this.properties.value[0] == 0) {
        //省       
        for (var i = 0; i < this.data.cityData.length; i++) {
          var item = this.data.cityData[i];
          if (item.id == this.data.value[0]) {
            tempProvincePos = i;
            break;
          }
        }
        //市
        if (this.data.cityData.length > tempProvincePos) {
          var cityList = this.data.cityData[tempProvincePos].childs;
          for (var i = 0; i < cityList.length; i++) {
            var item = cityList[i];
            if (item.id == this.data.value[1]) {
              tempCityPos = i;
              break
            }
          }
        }
        //区县
        if (this.data.isShowArea) {
          if (this.data.cityData.length > tempProvincePos) {
            var areaList = this.data.cityData[tempProvincePos].childs[tempCityPos].childs;
            for (var i = 0; i < areaList.length; i++) {
              var item = areaList[i];
              if (item.id == this.data.value[2]) {
                tempAreaPos = i;
                break
              }
            }
          }
        }
      } else {
        tempProvincePos = this.properties.value[0];
        tempCityPos = this.properties.value[1];
        tempAreaPos = this.properties.value[2];
      }

      this.setData({
        tempProvincePos: [tempProvincePos],
        tempCityPos: [tempCityPos],
        tempAreaPos: [tempAreaPos]
      })
    },
    onShow() {
      this.setData({
        showPicker: this.data.isShow
      })
    }
  }
});